#include "qmypushbutton.h"

#include "../../include/abstractproperty.h"
#include "../../lib/shared/qdatamanager.h"
#include "../../include/qcommonstruct.h"

#include <QVariant>
#include <QMouseEvent>

QButtonHost::QButtonHost(QWidget *wid):
    QObjectHost(wid)
{

}

QString QButtonHost::text()
{
    return ((QAbstractButton*)m_widget)->text();
}

void QButtonHost::setText(const QString &text)
{
    ((QAbstractButton*)m_widget)->setText(text);
}

bool QButtonHost::checked()
{
    return ((QAbstractButton*)m_widget)->isChecked();
}

QMyPushButton::QMyPushButton(QWidget *parent):
    QPushButton(parent),
    m_host(new QButtonHost(this))
{
    setProperty("className","QPushButton");
    setProperty("typeName","Push Button");
    setProperty("typeIcon",":/images/widgets/pushbutton.png");

    m_host->addProperty("Attribute",QStringList()<<"text"<<"font"<<"focusPolicy");
    m_host->addProperty("Signals",QStringList()<<"clicked");
    m_host->addProperty("StyleSheet",QStringList()<<"textColor"<<"disabled textColor"
                        <<"hover textColor"<<"pressed textColor"<<"image"<<"disabled image"
                        <<"hover image"<<"pressed image");
    m_host->addUserProperty(QStringList()<<"text"<<"toolTip");

    connect(this,SIGNAL(clicked()),this,SLOT(clicked_signals()));

}

QMyPushButton::~QMyPushButton()
{
    delete m_host;
}

void QMyPushButton::clicked_signals()
{
    if(property("running").toBool())
    {
        QString code=property("clicked").toString();
        if(code!="")
        {
            QString para=QString("type=clicked,num=0");
            m_host->exec(code,para);
        }
    }
}

void QMyPushButton::updateProperty(const QString & name)
{
    if(name=="Z-Value")
    {
        emit zValueChanged();
    }
    m_host->updateStyle(name,true);
}

void QMyPushButton::updatePropertyAttribute()
{
    m_host->updatePropertyAttribute();
    PropertyList list=property("property_list").value<PropertyList>();

    QString name;

    foreach(QAbstractProperty *p,list)
    {
        name=p->name();
        if(name=="image"
        || name=="disabled image"
        || name=="hover image"
        || name=="pressed image")
        {
            p->setAttribute("enabledList",QStringList()<<"0");
        }
    }
}

void QMyPushButton::initProperty()
{
    if(property("running").toBool())
    {
        m_host->setGlobalObject(sender());
        m_host->initRunningProperty();
    }
}

void QMyPushButton::updateStyleSheet()
{
    QString str;
    QMapIterator<QString,QString>   it(m_styleSheets);
    while(it.hasNext())
    {
        it.next();
        if(it.value()!="")
        {
            str+=it.value();
        }
    }
    setStyleSheet(str);
}


QMyRadioButton::QMyRadioButton(QWidget *parent):
    QRadioButton(parent),
    m_host(new QButtonHost(this))
{
    setProperty("className","QRadioButton");
    setProperty("typeName","Radio Button");
    setProperty("typeIcon",":/images/widgets/radiobutton.png");

    m_host->addProperty("Attribute",QStringList()<<"text"<<"checked"<<"font"<<"focusPolicy");
    m_host->addProperty("Signals",QStringList()<<"clicked");
    m_host->addProperty("StyleSheet",QStringList()<<"textColor"<<"disabled textColor"
                        <<"hover textColor"<<"pressed textColor");
    m_host->addProperty("Indicator",QStringList()<<"unchecked normal"<<"unchecked disabled"
                        <<"unchecked hover"<<"unchecked pressed"
                        <<"checked normal"<<"checked disabled"<<"checked hover"<<"checked pressed");
    m_host->addUserProperty(QStringList()<<"text"<<"toolTip"<<"checked");

    connect(this,SIGNAL(clicked()),this,SLOT(clicked_signals()));
    connect(this,SIGNAL(toggled(bool)),this,SLOT(toggle_signals()));

}

void QMyRadioButton::clicked_signals()
{
    if(property("running").toBool())
    {
        QString code=property("clicked").toString();
        if(code!="")
        {
            QString para=QString("type=clicked,num=1,_checked=%1").arg(this->isChecked()?"true":"false");
            m_host->exec(code,para);
        }
    }
}

void QMyRadioButton::toggle_signals()
{
    if(property("running").toBool())
    {
        QDataManager *manager=QDataManager::manager();
        MemeryValueItems items=property("memeryValue").value<MemeryValueItems>();
        foreach(tagMemeryValueInfo info,items)
        {
            if(info.m_propertyName=="checked")
            {
                manager->setUserData(info.m_valueName,this->isChecked());
            }
        }
    }
}

void QMyRadioButton::updateProperty(const QString & name)
{
    if(name=="Z-Value")
    {
        emit zValueChanged();
    }
    m_host->updateStyle(name,true);
}

void QMyRadioButton::updatePropertyAttribute()
{
    m_host->updatePropertyAttribute();
    PropertyList list=property("property_list").value<PropertyList>();

    QString name;

    foreach(QAbstractProperty *p,list)
    {
        name=p->name();

        if(name=="clicked")
        {
            p->setAttribute("param","clicked_bool");
        }
    }
}

void QMyRadioButton::initProperty()
{
    if(property("running").toBool())
    {
        m_host->setGlobalObject(sender());
        m_host->initRunningProperty();
    }
}



QMyCheckBox::QMyCheckBox(QWidget *parent):
    QCheckBox(parent),
    m_host(new QButtonHost(this))
{
    setProperty("className","QCheckBox");
    setProperty("typeName","Check Box");
    setProperty("typeIcon",":/images/widgets/checkbox.png");

    m_host->addProperty("Attribute",QStringList()<<"text"<<"checked"<<"font"<<"focusPolicy");
    m_host->addProperty("Signals",QStringList()<<"clicked");
    m_host->addProperty("StyleSheet",QStringList()<<"textColor"<<"disabled textColor"
                        <<"hover textColor"<<"pressed textColor");
    m_host->addProperty("Indicator",QStringList()<<"unchecked normal"<<"unchecked disabled"
                        <<"unchecked hover"<<"unchecked pressed"
                        <<"checked normal"<<"checked disabled"<<"checked hover"<<"checked pressed");


    m_host->addUserProperty(QStringList()<<"text"<<"toolTip"<<"checked");
    m_host->addStyleSheetGroup(QStringList()<<"Indicator");

    connect(this,SIGNAL(clicked()),this,SLOT(clicked_signals()));
}

void QMyCheckBox::clicked_signals()
{
    QString code=property("clicked").toString();
    if(code!="")
    {
        QString para=QString("type=clicked,num=1,_checked=%1").arg(this->isChecked()?"true":"false");
        m_host->exec(code,para);
    }
    if(property("running").toBool())
    {
        QDataManager *manager=QDataManager::manager();
        MemeryValueItems items=property("memeryValue").value<MemeryValueItems>();
        foreach(tagMemeryValueInfo info,items)
        {
            if(info.m_propertyName=="checked")
            {
                manager->setUserData(info.m_valueName,this->isChecked());

            }
        }
    }
}

void QMyCheckBox::updateProperty(const QString & name)
{
    if(name=="Z-Value")
    {
        emit zValueChanged();
    }
    m_host->updateStyle(name,true);
}


void QMyCheckBox::updatePropertyAttribute()
{
    m_host->updatePropertyAttribute();
    PropertyList list=property("property_list").value<PropertyList>();

    QString name;

    foreach(QAbstractProperty *p,list)
    {
        name=p->name();

        if(name=="clicked")
        {
            p->setAttribute("param","clicked_bool");
        }
    }
}

void QMyCheckBox::initProperty()
{
    if(property("running").toBool())
    {
        m_host->setGlobalObject(sender());
        m_host->initRunningProperty();
    }
}

void QMyCheckBox::updateStyleSheet()
{
    QString str;
    QMapIterator<QString,QString>   it(m_styleSheets);
    while(it.hasNext())
    {
        it.next();
        if(it.value()!="")
        {
            str+=it.value();
        }
    }
    setStyleSheet(str);
}
